﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using EtlComm;
using System.Diagnostics;

namespace ETLHandler
{
    class CSQLMap:IMap
    {
        #region IMap 成员

        public IDictionary<string, TableKey> Map(int TaskID)
        {
            IDictionary<string, TableKey> dic = new Dictionary<string, TableKey>();
            XElement XEStation = XElement.Load(EtlGlobal.XmlColumnsFile);
            var elements = from a in XEStation.Descendants("Task")
                          where a.Element("TaskID").Value == TaskID.ToString()
                          select a;
            foreach (var e in elements)
            {
                foreach (var xe in e.Elements("Columns"))
                {
                    string key = xe.Attribute("SrcCol").Value;
                    bool isPrimarykey = false;
                    if (xe.Attributes().Where(attr => attr.Name == "PK").Count() != 0)
                    {
                        isPrimarykey = xe.Attribute("PK").Value == "1";
                    }
                    TableKey tk = new TableKey
                    {
                        Name = xe.Attribute("DstCol").Value,
                        IsPrimaryKey = isPrimarykey
                    };
                    dic.Add(key, tk);
                }
            }
            return dic;
        }

        public string GetTableNameByTaskID(int TaskID)
        {
            XElement XEStation = XElement.Load(EtlGlobal.XmlColumnsFile);
            var elements = from a in XEStation.Descendants("Task")
                           where a.Element("TaskID").Value == TaskID.ToString()
                           select a;
            return elements.FirstOrDefault().Element("DstTable").Value;
        }

        #endregion
    }
}
